package leetcode

type MinStack struct {
	data []int
	min  int
}

/** initialize your data structure here. */
func Constructor() MinStack {
	return MinStack{
		data: make([]int, 0, 4),
	}
}

func (this *MinStack) Push(x int) {
	count := len(this.data)
	if count == 0 {
		this.min = x
		this.data = append(this.data, 0)
		return
	}

	this.data = append(this.data, x-this.min)
	if x < this.min {
		this.min = x
	}
}

func (this *MinStack) Pop() {
	count := len(this.data)
	if count == 0 {
		return
	}

	pop := this.data[count-1]
	if pop < 0 {
		this.min -= pop
	}
	this.data = this.data[:count-1]
}

func (this *MinStack) Top() int {
	count := len(this.data)
	if count == 0 {
		return 0
	}

	if top := this.data[count-1]; top > 0 {
		return this.min + top
	}
	return this.min
}

func (this *MinStack) GetMin() int {
	return this.min
}
